All files / application/services Container.js

100% Statements 1/1
100% Branches 2/2
100% Functions 4/4
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                1x  
/**
 * Dependency Injection Container
 * Supports singleton, transient, and scoped lifetimes
 */
class Container {
  constructor() {
    // TODO 1: Inicializáld a services Map-et (singleton instance-ok tárolása)
    // TODO 2: Inicializáld a factories Map-et (factory függvények tárolása)
    // TODO 3: Inicializáld a lifetimes Map-et (lifecycle típusok tárolása)
    
    // Példa inicializálás:
    // this.services = new Map();
    // this.factories = new Map();
    // this.lifetimes = new Map();
  }
 
  register(name, factory, lifetime = 'singleton') {
    // TODO 4: Tárold el a factory függvényt (this.factories.set(name, factory))
    // TODO 5: Tárold el a lifetime típust (this.lifetimes.set(name, lifetime))
    // TODO 6: Ha a lifetime === 'singleton', azonnal példányosítsd:
    //         - Hívd meg a factory-t: const instance = factory();
    //         - Tárold el: this.services.set(name, instance);
  }
 
  resolve(name, scope = null) {
    // TODO 7: Ha a service regisztrálva van mint 'scoped' ÉS van scope paraméter:
    //         - Ellenőrizd: if (scope && scope.has(name)) return scope.get(name);
    //         - Ha nincs még a scope-ban, példányosítsd és tárold: 
    //           const instance = this.factories.get(name)();
    //           scope.set(name, instance);
    //           return instance;
    
    // TODO 8: Ha singleton, add vissza a services-ből:
    //         if (this.lifetimes.get(name) === 'singleton') {
    //           return this.services.get(name);
    //         }
    
    // TODO 9: Ha transient, minden alkalommal hívj egy új factory-t:
    //         if (this.lifetimes.get(name) === 'transient') {
    //           return this.factories.get(name)();
    //         }
    
    // TODO 10: Ha nem regisztrált a service, dobj hibát:
    //          throw new Error(`Service '${name}' is not registered`);
  }
 
  createScope() {
    // TODO 11: Hozz létre egy új Map-et az scoped instance-oknak
    // TODO 12: Térj vissza egy objektummal ami tartalmaz egy resolve metódust:
    //          const scopeMap = new Map();
    //          return {
    //            resolve: (name) => this.resolve(name, scopeMap)
    //          };
  }
}
 
module.exports = Container;